public class Solution838 {
    public String pushDominoes(String dominoes) {
        StringBuilder stringBuilder=new StringBuilder();
        int start=0;
        while (dominoes.length()!=0) {
            int l = start-1, r = start-1;
            for (int i = start; i < dominoes.length(); i++) {
                if (dominoes.charAt(i) == 'R') {
                    r = i;
                    break;
                } else if (dominoes.charAt(i) == 'L') {
                    l = i;
                }
            }
            if (l == start-1 && r == start-1) {
                return stringBuilder.append(dominoes.substring(start)).toString();
            }
            for (int i = start; i <= l; i++) {
                stringBuilder.append('L');
            }
            if (r == start-1) {
                for (int i = l + 1; i < dominoes.length(); i++) {
                    stringBuilder.append('.');
                }
                return stringBuilder.toString();
            }
            for (int i = l + 1; i < r; i++) {
                stringBuilder.append('.');
            }
            l = r + 1;
            if (l == dominoes.length()) {
                stringBuilder.append('R');
                return stringBuilder.toString();
            }
            while (true) {
                if (dominoes.charAt(l) == 'L') {
                    break;
                } else if (dominoes.charAt(l) == 'R' || l == dominoes.length() - 1) {
                    for (int i = r; i < l; i++) {
                        stringBuilder.append('R');
                    }
                    if (l == dominoes.length() - 1) {
                        stringBuilder.append('R');
                        return stringBuilder.toString();
                    }
                    r = l;
                    l++;
                } else {
                    l++;
                }
            }
            int mid = (l + r) / 2;
            for (int i = r; i <= mid; i++) {
                stringBuilder.append('R');
            }
            if ((l + r) % 2 == 0) {
                stringBuilder.setCharAt(stringBuilder.length()-1,'.');
            }
            for (int i = mid + 1; i <= l; i++) {
                stringBuilder.append('L');
            }
            r = l + 1;
            start=r;
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution838().pushDominoes(".L.R...LR..L.."));
    }
}
